﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CPA_Monitor
{
    public partial class EditNetworks : Form
    {
        public bool ChangesOccured = false;

        public EditNetworks()
        {
            InitializeComponent();
            LoadEntries();
        }

        void LoadEntries()
        {
            Networks.Load();
            comboBoxNetworks.Items.Clear();
            ClearTexts();
            foreach (Networks.CPANetwork network in Networks.NetworkEntries)
            {
                comboBoxNetworks.Items.Add(network.Company);
            }
            if (comboBoxNetworks.Items.Count > 0) comboBoxNetworks.SelectedIndex = 0;

        }

        void LoadEntry(string company)
        {
            if (company != "" && company != null)
            {
                var network = Networks.NetworkEntries.Where(x => x.Company == company).ToList()[0];
                textBoxCompany.Text = network.Company;
                textBoxUsername.Text = network.Username;
                textBoxPassword.Text = network.Password;
                textBoxStatsURL.Text = network.StatsUrl;
                textBoxLoginURL.Text = network.LoginUrl;
                textBoxProfitRegex.Text = network.RegexProfit;
                textBoxClicksRegex.Text = network.RegexClicks;
                textBoxEPCRegex.Text = network.RegexEPC;
                textBoxLoginIndicator.Text = network.LoggedInIndicator;
                richTextBoxLoginPostKeys.Text = network.LoginPostDataKeys;
                richTextBoxLoginPostValues.Text = network.LoginPostDataValues;
                richTextBoxStatsPostKeys.Text = network.StatsPostDataKeys;
                richTextBoxStatsPostValues.Text = network.StatsPostDataValues;
                checkBoxEnabled.Checked = network.Enabled;
            }
        }

        void ClearTexts()
        {
            foreach (Control c in new Control[] { tabPage1, tabPage2, splitContainerPOSTLogin.Panel1, splitContainerPOSTLogin.Panel2, splitContainerPOSTStats.Panel1, splitContainerPOSTStats.Panel2 })
                foreach (Control f in c.Controls.Cast<Control>().Where(x => x is TextBox || x is RichTextBox))
                    f.Text = "";
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            ClearTexts();
            comboBoxNetworks.SelectedIndex = -1;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (textBoxCompany.Text == "")
            {
                MessageBox.Show("No company name specified");
                return;
            }
            Networks.CPANetwork newNetwork = null;
            foreach (Networks.CPANetwork network in Networks.NetworkEntries)
            {
                if (network.Company == textBoxCompany.Text)
                {
                    newNetwork = network;
                    break;
                }
            }
            if (newNetwork == null)
            {
                newNetwork = new Networks.CPANetwork();
                Networks.NetworkEntries.Add(newNetwork);
            }

            newNetwork.Company = textBoxCompany.Text;
            newNetwork.Username = textBoxUsername.Text;
            newNetwork.Password = textBoxPassword.Text;
            newNetwork.StatsUrl = textBoxStatsURL.Text;
            newNetwork.LoginUrl = textBoxLoginURL.Text;
            newNetwork.RegexProfit = textBoxProfitRegex.Text;
            newNetwork.RegexClicks = textBoxClicksRegex.Text;
            newNetwork.RegexEPC = textBoxEPCRegex.Text;
            newNetwork.LoginPostDataKeys = richTextBoxLoginPostKeys.Text;
            newNetwork.LoginPostDataValues = richTextBoxLoginPostValues.Text;
            newNetwork.Enabled = checkBoxEnabled.Checked;
            newNetwork.LoggedInIndicator = textBoxLoginIndicator.Text;
            newNetwork.StatsPostDataKeys = richTextBoxStatsPostKeys.Text;
            newNetwork.StatsPostDataValues = richTextBoxStatsPostValues.Text;

            Networks.Save();
            LoadEntries();
            comboBoxNetworks.SelectedItem = newNetwork.Company;
            ChangesOccured = true;
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Networks.NetworkEntries.RemoveAll(x => x.Company == comboBoxNetworks.Text);
            Networks.Save();
            LoadEntries();
        }

        private void comboBoxNetworks_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadEntry(comboBoxNetworks.Text);
        }
    }
}
